(function (vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addShopVipLevelInfo: {
                vipLevelId: '',
                levelName: '',
                vipLevel: '',
                remark: '',
                goldCostMoney: '1',
                integralCostMoney: '1',
                couponValues: []
            }
        },
        _initMethod: function () {
            vc.getDict('s_shop_vip_level', "vip_level", function (_data) {
                $that.addShopVipLevelInfo.vipLevels = _data;
            });
        },
        _initEvent: function () {
            vc.on('addShopVipLevel', 'openAddShopVipLevelModal', function () {
                $('#addShopVipLevelModel').modal('show');
            });
            vc.on('chooseCouponShopPool', 'chooseCouponShopPool', function (_couponShopPool) {
                $that._addTitleValue(_couponShopPool);
            });
        },
        methods: {
            addShopVipLevelValidate() {
                return vc.validate.validate({
                    addShopVipLevelInfo: $that.addShopVipLevelInfo
                }, {
                    'addShopVipLevelInfo.levelName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "会员名称太长"
                        },
                    ],
                    'addShopVipLevelInfo.vipLevel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员等级不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "会员等级错误"
                        },
                    ],
                    'addShopVipLevelInfo.goldCostMoney': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "优惠卷赠送不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "优惠卷赠送错误"
                        },
                    ],
                    'addShopVipLevelInfo.integralCostMoney': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "积分赠送不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "积分赠送错误"
                        },
                    ],
                    'addShopVipLevelInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "说明太长"
                        },
                    ]
                });
            },
            saveShopVipLevelInfo: function () {
                if (!$that.addShopVipLevelValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                console.log($that.addShopVipLevelInfo);
                var isFalse = false;
                $that.addShopVipLevelInfo.couponValues.forEach(item => {
                    if (item.couponCount == 0) {
                        vc.toast(item.couponName + '优惠卷个数不能为空');
                        isFalse = true;
                    }
                    if (item.couponCount > item.stock) {
                        vc.toast(item.couponName + '优惠卷库存不足');
                        isFalse = true;
                    }
                });
                if (isFalse) {
                    return;
                }
                $that.addShopVipLevelInfo.shopId = vc.getCurrentCommunity().shopId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addShopVipLevelInfo);
                    $('#addShopVipLevelModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/shopVipUser/saveShopVipLevel',
                    JSON.stringify($that.addShopVipLevelInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopVipLevelModel').modal('hide');
                            $that.clearAddShopVipLevelInfo();
                            vc.emit('shopVipLevelManage', 'listShopVipLevel', {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _openAddChooseCouponShopPool: function () {
                vc.emit('chooseCouponShopPool', 'openChooseCouponShopPoolModel', {});
            },
            _addTitleValue: function (_couponShopPool) {
                var _temCounponShopPool = [];
                $that.addShopVipLevelInfo.couponValues.forEach(item => {
                    if (item.spId == _couponShopPool.spId) {
                        item.couponCount = Number(item.couponCount) + 1;
                        _temCounponShopPool.push(item);
                    } else {
                        _temCounponShopPool.push(item);
                    }
                });
                console.log(_temCounponShopPool);
                if (_temCounponShopPool.length > 0) {
                    $that.addShopVipLevelInfo.couponValues = _temCounponShopPool;
                } else {
                    $that.addShopVipLevelInfo.couponValues.push(_couponShopPool);
                }
            },
            _checkStock: function (_coupon) {
                if (_coupon.couponCount > _coupon.stock) {
                    vc.toast('优惠卷库存不足');
                    return;
                }
            },
            _deleteTitleValue: function (_spId) {
                console.log(_spId);

                let _newTitleValues = [];
                let _tmpTitleValues = $that.addShopVipLevelInfo.couponValues;
                _tmpTitleValues.forEach(item => {
                    if (item.spId != _spId) {
                        _newTitleValues.push(item)
                    }
                });

                $that.addShopVipLevelInfo.couponValues = _newTitleValues;
            },
            _goEditBack: function () {
                vc.emit('shopVipLevelManage', 'listShopVipLevel', {});
            },
            clearAddShopVipLevelInfo: function () {
                $that.addShopVipLevelInfo = {
                    levelName: '',
                    vipLevel: '',
                    remark: '',
                    goldCostMoney: '1',
                    integralCostMoney: '1',
                    couponValues: []
                };
            }
        }
    });
})(window.vc);
